package edu.rit.hvz.android;

import java.util.LinkedList;
import java.util.List;

import edu.rit.hvz.android.DataLayer.Player;
import edu.rit.hvz.android.DataLayer.PlayerDBAdapter;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class LeaderBoards extends ListActivity {
	private static List<Player> activePlayers = new LinkedList<Player>();
	private Cursor mCursor; 
	private PlayerDBAdapter db = null;
	private SimpleCursorAdapter adapter;
	private EditText filterText = null;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.onActivityCreateSetTheme(this);
        setContentView(R.layout.leaderboard_layout);
        
        filterText = (EditText) findViewById(R.id.player_search);
        filterText.addTextChangedListener(filterTextWatcher);
        
        db = new PlayerDBAdapter(this);
        db.open();
        // Put a managed wrapper around the retrieved cursor so we don't have to worry about
        // requerying or closing it as the activity changes state.
        mCursor = db.getAllPlayers();
        startManagingCursor(mCursor);

        // Now create a new list adapter bound to the cursor.
        // SimpleListAdapter is designed for binding to a Cursor.
        adapter = new SimpleCursorAdapter(
                this, // Context.
                R.layout.player_list_item,  // Specify the row template to use (here, two columns bound to the two retrieved cursor rows).
                mCursor,                                              // Pass in the cursor to bind to.
                new String[] {PlayerDBAdapter.NAME, PlayerDBAdapter.TEAM, PlayerDBAdapter.CLAN},           // Array of cursor columns to bind to.
                new int[] {R.id.name, R.id.team, R.id.clan});  // Parallel array of which template objects to bind to those columns.

        adapter.setFilterQueryProvider(new FilterQueryProvider() {

            public Cursor runQuery(CharSequence constraint) {
                String partialValue = constraint.toString();
                return db.filterPlayersByName(partialValue);

            }
        });
        
        // Bind to our new adapter.
        setListAdapter(adapter);
        
    }
	
	private TextWatcher filterTextWatcher = new TextWatcher() {

	    public void afterTextChanged(Editable s) {
	    }

	    public void beforeTextChanged(CharSequence s, int start, int count,
	            int after) {
	    }

	    public void onTextChanged(CharSequence s, int start, int before,
	            int count) {
	        adapter.getFilter().filter(s);
	        
	    }

	};
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id){
		Intent intent = new Intent().setClass(this, PlayerProfile.class);
		intent.putExtra(Player.BUNDLE_KEY, id);
		
		
		this.startActivity(intent);
		
	}
}
